package com.mj.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.UUID;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.mj.MJApplaction;

public class Info {

	private static String sID = null;
	private static String versionName = null;
	private static int versionCode = 0;
	private static final String INSTALLATION = "INSTALLATION";

	/***
	 * 用户是否登录
	 */
	public static boolean isLogin() {
		if (MJApplaction.userInfo == null) {
			return false;
		}
		if (TextUtils.isEmpty(MJApplaction.userInfo.getToken()) || TextUtils.isEmpty(MJApplaction.userInfo.getUserkey())) {
			return false;
		}
		return true;
	}

	/***
	 * 保存用户信息
	 */
	public static void saveUserInfo(Context context) {
		// 保存用户信息
		SharedPreferences preferences = context.getSharedPreferences("user", Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString("token", MJApplaction.userInfo.getToken());
		editor.putString("userkey", MJApplaction.userInfo.getUserkey());
		editor.putString("id", MJApplaction.userInfo.getId());
		editor.putString("avatar", MJApplaction.userInfo.getAvatar());
		editor.putString("nickname", MJApplaction.userInfo.getNickname());
		editor.commit();
	}

	/**
	 * 获取版本名
	 * 
	 * @return 当前应用的版本名
	 */
	public static String getVersionName(Context context) {
		if (versionName == null) {
			PackageManager manager = context.getPackageManager();
			try {
				PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
				versionName = info.versionName;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
				return "none";
			}
		}
		return versionName;
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static int getVersionCode(Context context) {
		if (versionCode == 0) {
			PackageManager manager = context.getPackageManager();
			try {
				PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
				versionCode = info.versionCode;
			} catch (NameNotFoundException e) {
				e.printStackTrace();
				return 0;
			}
		}
		return versionCode;
	}

	/***
	 * 生成手机唯一标识,先检查IMEI号是否合法，如不合法，采用谷歌推荐的方法生成一个
	 * 
	 * @param context
	 * @return
	 */
	public synchronized static String getID(Context context) {
		if (sID == null) {
			TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			String id = tm.getDeviceId();
			if (!TextUtils.isEmpty(id)) {
				sID = id;
				return sID;
			}
			File installation = new File(context.getFilesDir(), INSTALLATION);
			try {
				if (!installation.exists()) {
					writeInstallationFile(installation);
				}
				sID = readInstallationFile(installation);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return sID;
	}

	private static String readInstallationFile(File installation) throws IOException {
		RandomAccessFile f = new RandomAccessFile(installation, "r");
		byte[] bytes = new byte[(int) f.length()];
		f.readFully(bytes);
		f.close();
		return new String(bytes);
	}

	private static void writeInstallationFile(File installation) throws IOException {
		FileOutputStream out = new FileOutputStream(installation);
		String id = UUID.randomUUID().toString();
		out.write(id.getBytes());
		out.close();
	}
}
